package com.punai.customer.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.common.exception.ServiceException;
import com.punai.common.utils.StringUtils;
import com.punai.customer.domain.CusGxFeatureConsume;
import com.punai.customer.domain.vo.CusGxFeatureConsumeVo;
import com.punai.customer.mapper.CusFeatureMapper;
import com.punai.customer.mapper.CusGxFeatureConsumeMapper;
import com.punai.customer.mapper.CusLinkSubpkConsumeMapper;
import com.punai.customer.service.ICusGxFeatureConsumeService;
import com.punai.gk.domain.vo.SimplifyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 标准费用项特征描述设置耗量Service业务层处理
 *
 * @author zhx
 * @date 2023-05-15
 */
@Service
public class CusGxFeatureConsumeServiceImpl extends ServiceImpl<CusGxFeatureConsumeMapper,CusGxFeatureConsume> implements ICusGxFeatureConsumeService
{
    @Autowired
    private CusGxFeatureConsumeMapper cusGxFeatureConsumeMapper;
    @Autowired
    private CusFeatureMapper cusFeatureMapper;
    @Autowired
    private CusLinkSubpkConsumeMapper cusLinkSubpkConsumeMapper;

    @Override
    public List<CusGxFeatureConsumeVo> getFeatureDescribe(CusGxFeatureConsume cusGxFeatureConsume) {
        String gxId = cusGxFeatureConsume.getGxId();
        Long companyId = cusGxFeatureConsume.getCompanyId();
        String address = cusGxFeatureConsume.getAddress();

        if(StringUtils.isEmpty(gxId)){
            throw new ServiceException("标准费用项id获取失败");
        }
        if(StringUtils.isEmpty(address)){
            throw new ServiceException("地区获取失败");
        }
        List<CusGxFeatureConsumeVo> voList = new ArrayList<>();

//        List<SimplifyVo> simplifyVos = cusFeatureMapper.selectIdNameList(gxId, companyId);
//        Map<String, String> featureIdNameMap = simplifyVos.stream().collect(Collectors.toMap(SimplifyVo::getId, SimplifyVo::getName));

        Map<String,String> featureMsgMap = new HashMap<>();
        List<CusGxFeatureConsume> cusGxFeatureConsumes = cusGxFeatureConsumeMapper.selectFeatureDescribe(gxId, companyId,address);
        for (CusGxFeatureConsume gxFeatureConsume : cusGxFeatureConsumes) {

            StringBuilder featureItem = new StringBuilder(gxFeatureConsume.getFeatureItem());
            Map<String,List<String>> featureItemMap = JSON.parseObject(featureItem.toString(), Map.class);
            featureItem = new StringBuilder();
            List<String> ids = featureItemMap.get("ids");
            List<String> items = featureItemMap.get("items");
            String lastFeatureId = "";
            for (int i = 0; i < ids.size(); i++) {
                String featureId = ids.get(i);
                if(StringUtils.isEmpty(featureId)){
                    continue;
                }
                String item = items.get(i);
                if(StringUtils.equals(lastFeatureId,featureId)){
                    featureItem.append("|").append(item);
                    continue;
                }
                String featureName;
                if(featureMsgMap.containsKey(featureId)){
                    featureName = featureMsgMap.get(featureId);
                }else {
                    featureName =  cusFeatureMapper.selectNameById(featureId, companyId);
                    if(StringUtils.isEmpty(featureName)){
                        featureName = "id:" + featureId + "被特征字典删除";
                    }
                    featureMsgMap.put(featureId,featureName);
                }
                featureItem.append(",").append(featureName).append(":").append(item);
                lastFeatureId = featureId;
            }
            CusGxFeatureConsumeVo vo = new CusGxFeatureConsumeVo();
            vo.setId(gxFeatureConsume.getId());
            vo.setGxId(gxFeatureConsume.getGxId());
            vo.setName(featureItem.substring(1));
            voList.add(vo);
        }

        return voList;
    }

    @Override
    @Transactional
    public int delFeatureDescribe(String id, Long companyId) {
        // 逻辑删除特征描述
        cusGxFeatureConsumeMapper.updateFeatureDescribeDelFlagIsTwo(id,companyId);
        cusLinkSubpkConsumeMapper.updateDelFlagIsTwo(id,companyId);
        return 200;
    }

    @Override
    @Transactional
    public int recoverFeatureDescribe(String gxId,String address, Long companyId) {
        // 恢复特征描述
        cusGxFeatureConsumeMapper.updateFeatureDescribeDelFlagIsZero(gxId,address,companyId);
        cusLinkSubpkConsumeMapper.updateDelFlagIsZeroAll(gxId,companyId);
        return 200;
    }
}
